package com.lsp.test;

import com.lsp.concurrent.locks.LSPCondition;
import com.lsp.concurrent.locks.LSPReentrantLock;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;

import java.util.Queue;
@Slf4j
public class ConditionNotify implements Runnable  {

    private Queue<String> bags;

    private int maxSize;

    private LSPReentrantLock lock;

    private LSPCondition condition;

    public ConditionNotify(Queue<String> bags, int maxSize, LSPReentrantLock lock, LSPCondition condition) {
        this.bags = bags;
        this.maxSize = maxSize;
        this.lock = lock;
        this.condition = condition;
    }

    @SneakyThrows
    @Override
    public void run() {
        int i = 0;
        while (true){
            i++;
            lock.lock();
            if(bags.size() == maxSize){
                log.info("bags满了！");
                condition.await();
            }
             Thread.sleep(1000);
            log.info("生产者生产：bag"+ i);
            bags.add("bag"+ i);
            condition.signal();
            lock.unlock();
        }

    }
}
